﻿
#If 0 Then
Imports osi.root.lock
Imports osi.root.lock.slimlock

Public Module _autolock
    Public Function make_autolock(Of T As islimlock)(ByRef i As T) As autolock(Of T)
        Return New autolock(Of T)(i)
    End Function
End Module

Public Structure auto_dual_read_lock
    Implements IDisposable

    Private ReadOnly i As duallock

    Public Sub New(ByRef i As duallock)
        Me.i = i
        i.p.reader_wait()
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        i.p.reader_release()
    End Sub
End Structure

Public Structure auto_dual_write_lock
    Implements IDisposable

    Private ReadOnly i As wrapper(Of duallock)

    Public Sub New(ByVal i As wrapper(Of duallock))
        assert(Not i Is Nothing)
        Me.i = i
        i.p.writer_wait()
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        i.p.writer_release()
    End Sub
End Structure

Public Structure autolock(Of l As islimlock)
    Implements IDisposable

    Private Shared ReadOnly need_box As Boolean = False

    Shared Sub New()
        need_box = Not GetType(l).IsClass()
    End Sub

    Private ReadOnly il As wrapper(Of l)

    Public Sub New(ByRef i As l)
        If need_box Then
            Me.io = i
        Else
            Me.il = i
        End If
        i.wait()
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        If need_box Then
            DirectCast(Me.io, l).release()
        Else
            Me.il.release()
        End If
    End Sub
End Structure
#End If
